
package utils;

import java.io.*;
import java.util.*;

public class ConsoleTextEditor {
    
    public static void save(BufferedReader in, String command) {
        
        try {
            String file;
            int space = command.indexOf(' ');
            if (space != -1)
                file = command.substring(space + 1);
            
            else {
                System.out.println(" Enter File name");
                file = in.readLine();
            }
            FileWriter outF = new FileWriter(file);
            PrintWriter outs = new PrintWriter(outF);
            System.out
                    .println("Enter as many lines as you like and type End to close the file.");
            // read and save data
            String dataLine = in.readLine();
            
            while (!("End".equals(dataLine))) {
                outs.println(dataLine);
                dataLine = in.readLine();
                
                if (dataLine == null || dataLine == "")
                    dataLine = "\n";
            }
            
            outs.close();
        }
        
        catch (IOException ex) {
            System.err.println("i/o error" + ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    public static void edit(String fileName) {
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(inputStreamReader);
        Vector vFile = new Vector();
        Vector section;
        String command;
        
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufFile = new BufferedReader(fileReader);
            
            String dataLine = bufFile.readLine();
            int lines = 1;
            
            while (dataLine != null) {
                // System.out.println(lines + " | " + dataLine);
                if (!vFile.add(dataLine))
                    break;
                dataLine = bufFile.readLine();
                lines++;
            }
            
            bufFile.close();
            while (true) {
                if (vFile.size() == 0) {
                    System.out.println("Arquivo vazio!");
                    System.out
                            .print("Digite:\n"
                                    +
                                    "'a' para adicionar texto em qualquer ponto\n"
                                    +
                                    "'s' para salvar e sair\n" +
                                    "Opção: ");
                } else {
                    for (int i = 0; i < vFile.size(); i++) {
                        System.out.println(String.valueOf(i + 1) + " | "
                                + vFile.get(i));
                    }
                    System.out
                            .print("Digite:\n"
                                    +
                                    "'l' para editar uma linha\n"
                                    +
                                    "'a' para adicionar texto em qualquer ponto\n"
                                    +
                                    "'r' para remover uma linha\n" +
                                    "'s' para salvar e sair\n" +
                                    "Opção: ");
                }
                command = in.readLine();
                if (command.charAt(0) == 's') {
                    break;
                } else if (command.charAt(0) == 'r') {
                    System.out.print("Digite a linha que deseja remover:");
                    command = in.readLine();
                    int line = Integer.parseInt(command.trim());
                    if (line <= vFile.size() && line > 0) {
                        vFile.removeElementAt(line - 1);
                    } else {
                        System.out.println("Linha inválida!");
                    }
                } else if (command.charAt(0) == 'a') {
                    System.out
                            .println("Digite as linhas que deseja adicionar ao documento: (Digite FIM para terminar)");
                    
                    section = new Vector();
                    
                    String toAdd = in.readLine();
                    
                    while (!("FIM".equals(toAdd))) {
                        section.add(toAdd);
                        toAdd = in.readLine();
                        
                        if (toAdd == null || dataLine == "")
                            toAdd = "\n";
                    }
                    
                    if (vFile.size() == 0) {
                        int line = 0;
                        
                        for (int i = section.size(); i > 0; i--) {
                            
                            vFile.insertElementAt(
                                    section.remove(section.size() - 1), line);
                        }
                    } else {
                        System.out
                                .print("Digite a linha em que deseja adicionar: (0 para início do documento)");
                        command = in.readLine();
                        int line = Integer.parseInt(command.trim());
                        
                        if (line <= vFile.size() && line >= 0) {
                            for (int i = section.size(); i > 0; i--) {
                                
                                vFile.insertElementAt(
                                        section.remove(section.size() - 1),
                                        line);
                            }
                        } else {
                            System.out.println("Linha inválida!");
                        }
                    }
                    
                } else if (command.charAt(0) == 'l') {
                    System.out
                            .print("Digite a linha que deseja editar: ");
                    command = in.readLine();
                    int line = Integer.parseInt(command.trim());
                    
                    if (line <= vFile.size() && line > 0) {
                        vFile.removeElementAt(line - 1);
                        
                        System.out
                                .print("Digite o novo conteúdo da linha: \n");
                        System.out.print(vFile.get(line - 1));
                        System.out.print("\n");
                        command = in.readLine();
                        
                        vFile.setElementAt(command, line - 1);
                        
                    } else {
                        System.out.println("Linha inválida!");
                    }
                }
            }
            
            FileWriter fileWriter = new FileWriter(fileName);
            PrintWriter printWriter = new PrintWriter(fileWriter);
            int size = vFile.size();
            
            for (int i = 0; i < size; i++) {
                String s = (String) vFile.remove(0);
                
                printWriter.println(s);
            }
            printWriter.close();
            
            System.out.print("Documento salvo: \n");
            print(fileName);
        }
        
        catch (IOException ex) {
            System.err.println("i/o error" + ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    public static void print(String file) {
        
        try {
            FileReader inS = new FileReader(file);
            BufferedReader Ins = new BufferedReader(inS);
            // read and print data
            String dataLine = Ins.readLine();
            int count = 1;
            
            while (dataLine != null) {
                System.out.println(dataLine);
                dataLine = Ins.readLine();
                count++;
            }
            if (count == 1) {
                System.out.println("Arquivo vazio!");
            }
            Ins.close();
        }
        
        catch (IOException ex) {
            System.err.println("i/o error" + ex.getMessage());
            ex.printStackTrace();
        }
    }
}
